﻿#include "ToolbarNosepiece.h"
#include <QDebug>
#include <QActionGroup>
namespace Qly
{
ToolBarNosepiece::ToolBarNosepiece(QWidget *parent)
    :QToolBar(parent), m_currentLens(-1)
{
    setIconSize(QSize(32, 32));
//    for(int i = 0; i < 13; i++)
//    {
//        icon[i] = new QIcon;
//    }:/res/null.png

    icon[LENS_NULL].addFile(QStringLiteral(":/res/null.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_1X].addFile(QStringLiteral(":/res/x1.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_2X].addFile(QStringLiteral(":/res/x2.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_4X].addFile(QStringLiteral(":/res/x4.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_5X].addFile(QStringLiteral(":/res/x5.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_10X].addFile(QStringLiteral(":/res/x10.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_20X].addFile(QStringLiteral(":/res/x20.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_40X].addFile(QStringLiteral(":/res/x40.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_50X].addFile(QStringLiteral(":/res/x50.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_80X].addFile(QStringLiteral(":/res/x80.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_100X].addFile(QStringLiteral(":/res/x100.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_150X].addFile(QStringLiteral(":/res/x150.png"), QSize(), QIcon::Normal, QIcon::Off);
    icon[LENS_2_5X].addFile(QStringLiteral(":/res/x2_5.png"), QSize(), QIcon::Normal, QIcon::Off);

    initContent();
    initConnection();
}

void ToolBarNosepiece::attach(LensManager *pManager)
{
    if(pManager == nullptr) return;
    int N = pManager->lensCount();
    setLensCount(N);
    for(int i = 0; i < N; i++)
    {
        LensData &data = pManager->lensByIndex(i);

        int iconIndex = qBound(0, (int)data.lens, (int)Qly::LENS::LENS_150X);

        qDebug() << "i = " << i << ", lens = " << iconIndex;
        actionLens[i]->setIcon(icon[iconIndex]);
        actionLens[i]->setToolTip(data.displayName);
    }
    connect(pManager, &Qly::LensManager::lensChanged, this, qOverload<int, int>(&Qly::ToolBarNosepiece::setLens));
    connect(this, &ToolBarNosepiece::changeLens, pManager, qOverload<int, int>(&LensManager::setLens));
    setLens(pManager->currentLensIndex());
}

void ToolBarNosepiece::initContent()
{
    actionGroup = new QActionGroup(this);
    for(int i = 0; i < MAX_LENS; i++)
    {
        actionLens[i] = new QAction(this);
        actionLens[i]->setObjectName(QString("Lens_%1").arg(i));
        actionLens[i]->setCheckable(true);
        addAction(actionLens[i]);
        actionGroup->addAction(actionLens[i]);
    }

    actionLens[0]->setIcon(icon[LENS_5X]);
    actionLens[1]->setIcon(icon[LENS_10X]);
    actionLens[2]->setIcon(icon[LENS_20X]);
    actionLens[3]->setIcon(icon[LENS_50X]);
    actionLens[4]->setIcon(icon[LENS_100X]);
    actionLens[5]->setIcon(icon[LENS_150X]);

    actionLens[0]->setToolTip("5X");
    actionLens[1]->setToolTip("10X");
    actionLens[2]->setToolTip("20X");
    actionLens[3]->setToolTip("50X");
    actionLens[4]->setToolTip("100X");
    actionLens[5]->setToolTip("150X");
    setWindowTitle("Lens select toolbar");
}

void ToolBarNosepiece::initConnection()
{
    for(int i = 0; i < 6; i++)
    {
        connect(actionLens[i], SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
    }
//    connect(actionLens0, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
//    connect(actionLens1, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
//    connect(actionLens2, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
//    connect(actionLens3, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
//    connect(actionLens4, SIGNAL(triggered()), this, SLOT(lensUpdate(void)));
 //   connect(actionLensHome, SIGNAL(triggered()), this, SLOT(lensHomeUI(void)));
}

void ToolBarNosepiece::lensHomeUI()
{
    setLens(0);
    qDebug() << "emit doLensHome()";
    emit doLensHome();
}

void ToolBarNosepiece::lensUpdate(void)
{

    qDebug() <<  "ToolBarNosepiece::lensUpdate(void)";
    QAction *act = (QAction *) sender();
    int lens;

    act->setChecked(true);
    for(int i = 0; i < m_lensCount; i++)
    {
        if(act == actionLens[i] )
        {
            lens = i;
            if(m_currentLens == lens)
            {
                return;
            }
            // 表示真正切换了物镜
            int oldLens = m_currentLens; // 这里要记录 oldlens, 因为 setLens 会改变 m_lens
            m_currentLens = lens;
            qDebug() << "ToolBarNosepiece::lensUpdate() changeLens(" << oldLens << ", " << lens << ")";
            emit changeLens(oldLens, lens);
            break;
        }
    }

}

void ToolBarNosepiece::setLens(int oldLens, int newLens)
{
    if(newLens == m_currentLens) return;
    qDebug() << "ToolBarNosepiece::setLens(int oldLens, int newLens)  oldLens = " << oldLens << ", newLens = " << newLens;

    if(oldLens == -1)
    {
        m_currentLens = -1; // 无论 m_lens 原来的值是多少。都默认这个动作是初始化物镜位置。
    }
    setLens(newLens);
}

void ToolBarNosepiece::nextLens(void)
{
    if(m_currentLens >= m_lensCount || m_currentLens < -1) return;
    setLens(m_currentLens, m_currentLens + 1);
}

void ToolBarNosepiece::previousLens(void)
{
    if(m_currentLens > m_lensCount || m_currentLens <= -1) return;
    if(m_currentLens == 0) return;
    setLens(m_currentLens, m_currentLens - 1);
}

void ToolBarNosepiece::setReadOnly(bool on)
{
//    for(int i = 0; i < 6; i++)
//    {
//        actionLens[i]->setCheckable(!on);
//    }
}

void ToolBarNosepiece::setLensMag(int lens, LENS mag)
{
    QIcon icon1Len1;
    actionLens[lens]->setIcon(icon[mag]);
}

void ToolBarNosepiece::setLensCount(int N)
{
    for(int i = 0; i < MAX_LENS; i++)
    {
        actionLens[i]->setVisible(false);
    }

    for(int i = 0; i < N; i++)
    {
        actionLens[i]->setVisible(true);
    }
    m_lensCount = N;
}

void ToolBarNosepiece::setLens(int lens)
{
    if(lens >= m_lensCount || lens <= -1) return;
    if(lens == -1)
    {
//        for(int i = 0; i < m_lensCount; i++)
//        {
//            actionLens[i]->setChecked(false);
//        }
//        return;
    }

    if(lens == m_currentLens) return;
//    for(int i = 0; i < m_lensCount; i++)
//    {
//        if(i != lens) actionLens[i]->setChecked(false);
//    }
    actionLens[lens]->setChecked(true);
    int old = m_currentLens;
    m_currentLens = lens;
    //if(old == -1) return; // 这表明是初始化，不需要再发信号出去了
    emit changeLens(old, lens);
}

} //namespace Qly
